package com.hornet.manager.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hornet.commons.web.BaseController;
import com.hornet.commons.web.model.AjaxResponse;
import com.hornet.oauth.sdk.weibo.Users;
import com.hornet.oauth.sdk.weibo.model.User;
import com.hornet.oauth.sdk.weibo.model.WeiboException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.PageFans;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.PageFansBean;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.javabeans.weibo.Company;
import com.qq.connect.oauth.Oauth;

@Controller
@RequestMapping("/manager/login")
public class LoginController extends BaseController {

	/**
	 * 微博
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "sina_weibo_login", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse sina_weibo_login(HttpServletRequest request, HttpSession session) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		try {
			System.out.println("使用微博登录....");
			String curennt_url = request.getHeader("Referer");
			System.out.println("当前URL：" + curennt_url);

			com.hornet.oauth.sdk.weibo.Oauth oauth = new com.hornet.oauth.sdk.weibo.Oauth();
			ajaxResponse.setMessage(oauth.authorize("code"));
			System.out.println(oauth.authorize("code"));
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return ajaxResponse;
	}

	@RequestMapping(value = "sina_weibo_callback", method = RequestMethod.GET)
	public void weibo_callback(@RequestParam String code) {
		try {
			System.out.println("微博验证回调....");
			System.out.println("code：" + code);

			com.hornet.oauth.sdk.weibo.Oauth oauth = new com.hornet.oauth.sdk.weibo.Oauth();
			com.hornet.oauth.sdk.weibo.http.AccessToken accessToken = oauth.getAccessTokenByCode(code);

			String uid = accessToken.getUid();
			Users users = new Users(accessToken.getAccessToken());
			User user = users.showUserById(uid);

			System.out.println(accessToken);

			String username = user.getScreenName(); // 用户呢称
			String avatar = user.getAvatarLarge(); // 用户头像
			String gender = user.getGender(); // 性别
			if ("m".equals(user.getGender())) {
				gender = "男";
			} else if ("f".equals(user.getGender())) {
				gender = "女";
			} else {
				gender = "未知";
			}

			System.out.println(username);
			System.out.println(avatar);
			System.out.println(gender);
			System.out.println();

		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	/**
	 * QQ
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "qq_login", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse qq_login(HttpSession session) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		try {
			System.out.println("使用QQ登录....");
			String curennt_url = request.getHeader("Referer");
			System.out.println("当前URL：" + curennt_url);

			response.setContentType("text/html;charset=utf-8");
			String redirect_url = new Oauth().getAuthorizeURL(request);
			ajaxResponse.setMessage(redirect_url);
			System.out.println(redirect_url);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajaxResponse;
	}

	@RequestMapping(value = "qq_callback", method = RequestMethod.GET)
	public void qq_callback() {
		response.setContentType("text/html; charset=utf-8");

		try {
			PrintWriter out = this.response.getWriter();
			AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);

			String accessToken = null, openID = null;
			long tokenExpireIn = 0L;

			if (accessTokenObj.getAccessToken().equals("")) {
				// 我们的网站被CSRF攻击了或者用户取消了授权
				// 做一些数据统计工作
				System.out.print("没有获取到响应参数");
			} else {
				accessToken = accessTokenObj.getAccessToken();
				tokenExpireIn = accessTokenObj.getExpireIn();

				request.getSession().setAttribute("demo_access_token", accessToken);
				request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));

				// 利用获取到的accessToken 去获取当前用的openid -------- start
				OpenID openIDObj = new OpenID(accessToken);
				openID = openIDObj.getUserOpenID();

				out.println("欢迎你，代号为 " + openID + " 的用户!");
				request.getSession().setAttribute("demo_openid", openID);
				out.println("<a href=" + "/shuoshuoDemo.html" + " target=\"_blank\">去看看发表说说的demo吧</a>");
				// 利用获取到的accessToken 去获取当前用户的openid --------- end

				out.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- start </p>");
				UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
				UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
				out.println("<br/>");
				if (userInfoBean.getRet() == 0) {
					out.println(userInfoBean.getNickname() + "<br/>");
					out.println(userInfoBean.getGender() + "<br/>");
					out.println("黄钻等级： " + userInfoBean.getLevel() + "<br/>");
					out.println("会员 : " + userInfoBean.isVip() + "<br/>");
					out.println("黄钻会员： " + userInfoBean.isYellowYearVip() + "<br/>");
				} else {
					out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
				}
				out.println("<p> end -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- end </p>");

				out.println("<p> start ----------------------------------- 验证当前用户是否为认证空间的粉丝------------------------------------------------ start <p>");
				PageFans pageFansObj = new PageFans(accessToken, openID);
				PageFansBean pageFansBean = pageFansObj.checkPageFans("97700000");
				if (pageFansBean.getRet() == 0) {
					out.println("<p>验证您" + (pageFansBean.isFans() ? "是" : "不是") + "QQ空间97700000官方认证空间的粉丝</p>");
				} else {
					out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + pageFansBean.getMsg());
				}
				out.println("<p> end ----------------------------------- 验证当前用户是否为认证空间的粉丝------------------------------------------------ end <p>");

				out.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在微博的昵称等信息 ---------------------------- start </p>");
				com.qq.connect.api.weibo.UserInfo weiboUserInfo = new com.qq.connect.api.weibo.UserInfo(accessToken, openID);
				com.qq.connect.javabeans.weibo.UserInfoBean weiboUserInfoBean = weiboUserInfo.getUserInfo();
				if (weiboUserInfoBean.getRet() == 0) {
					// 获取用户的微博头像----------------------start
					out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL30() + "/><br/>");
					out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL50() + "/><br/>");
					out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL100() + "/><br/>");
					// 获取用户的微博头像 ---------------------end

					// 获取用户的生日信息 --------------------start
					out.println("<p>尊敬的用户，你的生日是： " + weiboUserInfoBean.getBirthday().getYear() + "年" + weiboUserInfoBean.getBirthday().getMonth() + "月" + weiboUserInfoBean.getBirthday().getDay() + "日");
					// 获取用户的生日信息 --------------------end

					StringBuffer sb = new StringBuffer();
					sb.append("<p>所在地:" + weiboUserInfoBean.getCountryCode() + "-" + weiboUserInfoBean.getProvinceCode() + "-" + weiboUserInfoBean.getCityCode() + weiboUserInfoBean.getLocation());

					// 获取用户的公司信息---------------------------start
					ArrayList<Company> companies = weiboUserInfoBean.getCompanies();
					if (companies.size() > 0) {
						// 有公司信息
						for (int i = 0, j = companies.size(); i < j; i++) {
							sb.append("<p>曾服役过的公司：公司ID-" + companies.get(i).getID() + " 名称-" + companies.get(i).getCompanyName() + " 部门名称-" + companies.get(i).getDepartmentName() + " 开始工作年-" + companies.get(i).getBeginYear() + " 结束工作年-" + companies.get(i).getEndYear());
						}
					} else {
						// 没有公司信息
					}
					// 获取用户的公司信息---------------------------end

					out.println(sb.toString());

				} else {
					out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + weiboUserInfoBean.getMsg());
				}

				out.println("<p> end -----------------------------------利用获取到的accessToken,openid 去获取用户在微博的昵称等信息 ---------------------------- end </p>");

			}
		} catch (Exception e) {
		}
	}
}
